// pages/user/card.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    vipData:{},
    vip_id:-1,
    userInfo:{},
    show_open_vip:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    this.setData({ extConfig: app.getExtConfigSync() })
    app.getUserInfo(function (userInfo) {
      if (!userInfo.store_member.user_tag){
         var show_open_vip = 1;
      }
      that.setData({ userInfo: userInfo, show_open_vip: show_open_vip}) ;
        
      console.log(userInfo);
    })
    this.vipInfo();
  },
  selectVip:function(e){
    var name = e.currentTarget.dataset.name;
    this.setData({
      vip_id:name
    }); 
    var vipInfo = this.data.vipData.list[this.data.vip_id];
    this.setData({
      vipInfo: vipInfo,
    });
    
  },
  openVip:function(){
    if (this.data.vip_id<0){
      app.alert("请先选择会员类型");
      return false;
    }
    
    var vipInfo = this.data.vipData.list[this.data.vip_id];
    if (vipInfo.type =="free"){
      this.openFree();
    } else if (vipInfo.type == "charge"){
      this.openCharge();
    }
  },
  openCharge:function(){
    var vipInfo = this.data.vipData.list[this.data.vip_id];
    var chargeSet = vipInfo.openSetting.chargeSet;
    this.data.recharge_id = chargeSet;
    this.doRecharge();
  },
  suc:function(){
    app.alert("恭喜您,会员开通成功!");
    app.setPageData("user/index", { reload: 1 });
    wx.navigateBack();
  },
//充值
  doRecharge: function () {
    var that = this;
    that.setData({ loading: 1, disabled: 1 });
    that.data.payloading = 1;
    app.api("/api/pay/recharge", {
      recharge_id: this.data.recharge_id,
    }, function (res) {
      if (res.code != 200) {
        app.alert(res.msg);
        that.data.payloading = 0;
        that.setData({ 'loading': 0 });
        that.setData({ 'disabled': 0 });
        return false;
      }
      var order_sn = res.data.order_sn;
      app.api("api/pay/topay", {
        order_sn: order_sn
      }, function (res) {
        if (res.code != 200) {
          app.alert(res.msg);
          that.data.payloading = 0;
          that.setData({ 'loading': 0 });
          that.setData({ 'disabled': 0 });
          return false;
        }

        var params = res.data;
        //params.total_fee = 0.1;
        params.success = function (res) {
          that.setData({ loading: 0, disabled: 0 });
          that.suc();
        }
        params.fail = function (res) {
          console.log(res);
          if (res.errMsg == 'requestPayment:fail cancel') {
            that.setData({ loading: 0, disabled: 0 });
            return false;
          }
          app.alert(res.errMsg);
        }
        console.log(params);
        wx.requestPayment(params)
      })
    })
  },
  openFree: function(){
    var that = this;
    app.api("api/user/openVip", {
      vip_id: this.data.vip_id
    }, function (res) {
      if (res.code != 200) {
        app.alert(res.msg);
        return;
      }
      that.suc();
    })
  },
  vipInfo: function () {
    var that = this;
    app.api("mall/promotion/find", {
      name: 'VIP_Discounts'
    }, function (res) {
      if (res.code != 200) {
        app.alert(res.msg);
        return;
      }
      that.setData({
        vipData: res.data
      });
      console.log("vipData", that.data.vipData);

    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})